<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018\7\17 0017
 * Time: 10:03
 */
namespace api;
use controller;
use model\goodsModel;
use model\tool\imageModel;

class goods extends controller{


    public function __construct($container)
    {
        parent::__construct($container);
//        header('Access-Control-Allow-Origin:*');
    }

    public function index(){

        $key = $_GET['key'];

        $category_id = $_GET['category'];

        $params = [
            'key'=>$key,
            'category_id'=>$category_id,
        ];

        $list = goodsModel::search( $params );

        foreach( $list as &$goods ){

            if(!empty( $goods['image'] ) ){
//                $goods['image'] = WEB_URL . $goods['image'];

//                $image = new imageModel;

                $goods['image'] = imageModel::resize( $goods['image'], 228,228 );


            }else{
                $goods['image'] = WEB_URL . '/image/no_image.png';
            }


        }

        echo json_encode( $list );


    }

    public function get_list(){

    }

    public function info(){


        $goods_id = $_GET['goods_id'];

        $goods = goodsModel::detail( $goods_id );
        $goods['description'] = html_entity_decode( $goods['description'] );

        $goods['image_list'] = goodsModel::image_list( $goods_id );
        if( !empty( $goods['image_list'] ) ){
//            $imageMdl = new imageModel;
            foreach( $goods['image_list'] as &$img ){

                $img['url'] = imageModel::resize( $img['url'], 375,375 );

            }
        }



        echo json_encode( $goods );

    }

    /**
     * 用来进行测试， 之后会删除掉
     */
    public function add(){

        $price = $_GET['price'];
        $name = $_GET['name'];
        $r = goodsModel::add(  $name, $price );

        $res = [
            'status' => 0,
            'description'=> '添加成功'
        ];

        //todo 添加到数据库

        echo json_encode( $res );

    }

    /**
     * 用来进行测试， 之后会删除掉
     */
    public function delete(){
        $res = [
            'status' => 0,
            'description'=> '删除成功'
        ];

        $id = $_GET['id'];

        $r = goodsModel::delete( $id );


        if( empty( $r->num_rows ) ){
            $res = [
                'status' => 1,
                'description'=> '删除失败没找到相关数据'
            ];
        }


        //todo 添加到数据库

        echo json_encode( $res );

    }

    public function buy(){

        $res = [
            'status' => 0
        ];

        if( empty( $_GET['product_id'] ) ){

            $res['status'] = 1;
            $res['description'] = '请选定商品';
            die( json_encode( $res ) );

        }

        $goods_id = $_GET['product_id'];
        $goods = goodsModel::detail( $goods_id );

        if( empty( $goods ) ){

            $res['status'] = 1;
            $res['description'] = '商品不存在';
            die( json_encode( $res ) );

        }

        $goods_num = $_GET['number'];

        $_SESSION['buy'] = [
            'goods_id' => $goods_id,
            'goods_num' => $goods_num
        ];

        echo json_encode( $res );
    }
}